package com.qk.controller;

import com.qk.Dto.BusinessDto;
import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Business;
import com.qk.server.BusinessesServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/businesses")
@Slf4j
public class BusinessesController {
    @Autowired
    private BusinessesServer businessesServer;
    //添加数据
    @Log
    @PostMapping
    public Result add(@RequestBody Business business){
        log.info("添加数据{}", business);
        business.setStatus(1);
        business.setUpdateTime(LocalDateTime.now());
        business.setCreateTime(LocalDateTime.now());
        businessesServer.save(business);
        return Result.success();
    }
    //查询列表
    @GetMapping
    public Result findBusinessByList(BusinessDto businessDto){
        PageResult<Business> result =businessesServer.findBusinessByList(businessDto);
        return Result.success(result);
    }
    //分配商机
    @Log
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId){
        log.info("分配商机{}给{}", businessId, userId);
        Business business = new Business();
        business.setStatus(2);
        business.setId(businessId);
        business.setUserId(userId);
        businessesServer.updateById(business);
        return Result.success();
    }
    //根据Id查询商机
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        return Result.success(businessesServer.getBusinessById(id));
    }
    //跟进商机
    @Log
    @PutMapping
    public Result updateBusiness(@RequestBody Business business){
        log.info("跟进商机{}", business);
        businessesServer.updateBusinessById(business);
        return Result.success();
    }
    //踢回公海
    @Log
    @PutMapping("/back/{id}")
    public Result backBusinesses(@PathVariable Integer id){
        log.info("踢回公海{}", id);
        Business businessById = businessesServer.getBusinessById(id);
        businessById.setStatus(4);
        businessById.setUpdateTime(LocalDateTime.now());
        businessesServer.updateById(businessById);
        return Result.success();
    }
    //转客户
    @Log
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        log.info("转客户{}", id);
        businessesServer.toCustomer(id);
        return Result.success();
    }
    //公海列表查询
    @GetMapping("/pool")
    public Result findBusinessPoolList(BusinessDto businessDto){
        log.info("公海列表查询{}", businessDto);
        return Result.success(businessesServer.findBusinessPoolList(businessDto));
    }
}
